<template>
  <div>
    <el-table :data="tableData.data">
      <el-table-column prop="shopName" label="商品名称"></el-table-column>
      <el-table-column prop="shopAddress" label="商品地址"></el-table-column>
      <el-table-column prop="shopDescription" label="商品描述"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button @click="del(row.id)">删除</el-button>
          <el-button @click="bj(row)">编辑</el-button>
          <el-button @click="add">添加</el-button>
          <el-button @click="xq(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="show" title="添加/编辑">
      <el-form :model="show">
        <el-form-item label="商品名称" prop="shopName">
          <el-input v-model="forData.shopName" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品地址" prop="shopAddress">
          <el-input v-model="forData.shopAddress" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="shopDescription">
          <el-input v-model="forData.shopDescription" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="ok">确定</el-button>
          <el-button @click="show = flase">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import axios from 'axios'

const show = ref(false)
const index = ref(true)

const forData = ref({
  shopName: '',
  shopAddress: '',
  shopDescription: ''
})

const add = () => {
  show.value = true
  index.value = true
  forData.value = {
    shopName: '',
    shopAddress: '',
    shopDescription: ''
  }
}

const bj = (row) => {
  show.value = true
  index.value = false
  forData.value = row
}

const xq = (row) => {
  show.value = true
  // index.value = false
  forData.value = row
}
const tableData = reactive({
  data: [],
  total: 0
})

const ok = async () => {
  if (index.value) {
    await axios.put('/api/list', forData.value)
    console.log('添加')
  } else {
    await axios.post('/api/list', forData.value)
    console.log('编辑')
  }
  show.value = false
  await getList()
}

const getList = async () => {
  const res = await axios.get('/api/list')
  console.log(res)
  const { data, total } = res.data.data
  tableData.data = data
  tableData.total = total
}
getList()

const del = async (id) => {
  const res = await axios.delete('/api/list?id=' + id)
  console.log(res)
  alert('删除成功')
  await getList()
}
</script>